package com.zenithmind.coding.service.impl;

import com.zenithmind.coding.mapper.ProblemStatisticsMapper;
import com.zenithmind.coding.mapper.ProblemStatisticsMapper.ProblemStatistics;
import com.zenithmind.coding.mapper.ProblemStatisticsMapper.TagStatistics;
import com.zenithmind.coding.mapper.ProblemStatisticsMapper.UserSolveStatistics;
import com.zenithmind.coding.service.ProblemStatisticsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 问题统计服务实现类 - 遵循单一职责原则，专门处理问题统计相关功能
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ProblemStatisticsServiceImpl implements ProblemStatisticsService {

    private final ProblemStatisticsMapper problemStatisticsMapper;

    @Override
    public ProblemStatistics getProblemStatistics(String categoryId, Integer difficulty) {
        log.info("获取问题统计: categoryId={}, difficulty={}", categoryId, difficulty);
        return problemStatisticsMapper.selectProblemStatistics(categoryId, difficulty);
    }

    @Override
    public List<TagStatistics> getHotTags(Integer limit) {
        log.info("获取热门标签: limit={}", limit);
        return problemStatisticsMapper.selectHotTags(limit);
    }

    @Override
    public UserSolveStatistics getUserSolveStatistics(String userId) {
        log.info("获取用户解题统计: userId={}", userId);
        return problemStatisticsMapper.selectUserSolveStatistics(userId);
    }

    @Override
    public Object getDifficultyDistribution(String categoryId) {
        log.info("获取难度分布统计: categoryId={}", categoryId);
        return problemStatisticsMapper.selectDifficultyDistribution(categoryId);
    }

    @Override
    public Object getTypeDistribution(String categoryId) {
        log.info("获取类型分布统计: categoryId={}", categoryId);
        return problemStatisticsMapper.selectTypeDistribution(categoryId);
    }

    @Override
    public Object getSubmissionTrend(Integer days, String problemId) {
        log.info("获取提交趋势: days={}, problemId={}", days, problemId);
        return problemStatisticsMapper.selectSubmissionTrend(days, problemId);
    }

    @Override
    public Object getAcceptanceRateStatistics(String categoryId, Integer difficulty) {
        log.info("获取通过率统计: categoryId={}, difficulty={}", categoryId, difficulty);
        return problemStatisticsMapper.selectAcceptanceRateStatistics(categoryId, difficulty);
    }

    @Override
    public Object getLeaderboard(String type, Integer limit) {
        log.info("获取排行榜: type={}, limit={}", type, limit);
        return problemStatisticsMapper.selectLeaderboard(type, limit);
    }

    @Override
    public Object getUserActivityStatistics(String userId, Integer days) {
        log.info("获取用户活跃度统计: userId={}, days={}", userId, days);
        return problemStatisticsMapper.selectUserActivityStatistics(userId, days);
    }

    @Override
    public Object getUserLearningProgress(String userId, String categoryId) {
        log.info("获取用户学习进度: userId={}, categoryId={}", userId, categoryId);
        return problemStatisticsMapper.selectUserLearningProgress(userId, categoryId);
    }

    @Override
    public Object getUserAbilityRadar(String userId) {
        log.info("获取用户能力雷达图: userId={}", userId);
        return problemStatisticsMapper.selectUserAbilityRadar(userId);
    }

    @Override
    public Object getUserGrowthCurve(String userId, Integer days) {
        log.info("获取用户成长曲线: userId={}, days={}", userId, days);
        return problemStatisticsMapper.selectUserGrowthCurve(userId, days);
    }

    @Override
    public Object getGlobalStatistics() {
        log.info("获取全局统计");
        return problemStatisticsMapper.selectGlobalStatistics();
    }

    @Override
    public Boolean updateProblemStatistics(String problemId, Long submitCount, Long acceptCount) {
        log.info("更新问题统计信息: problemId={}, submitCount={}, acceptCount={}", problemId, submitCount, acceptCount);
        int result = problemStatisticsMapper.updateStatistics(problemId, submitCount, acceptCount);
        return result > 0;
    }

    @Override
    public Boolean updateProblemAvgPerformance(String problemId, Integer avgTime, Integer avgMemory) {
        log.info("更新问题平均性能: problemId={}, avgTime={}, avgMemory={}", problemId, avgTime, avgMemory);
        int result = problemStatisticsMapper.updateAvgPerformance(problemId, avgTime, avgMemory);
        return result > 0;
    }
}
